package hypernexusnats

import (
	"time"

	"github.com/nats-io/nats.go"
)

// INatsClient 定义NATS客户端接口
type INatsClient interface {
	// Publish 发布消息到指定主题
	Publish(subject string, data []byte) error

	// PublishWithReply 发布消息并等待回复
	PublishWithReply(subject string, reply string, data []byte) error

	// Request 发送请求并等待响应
	Request(subject string, data []byte, timeout time.Duration) (*nats.Msg, error)

	// Subscribe 订阅主题
	Subscribe(subject string, callback nats.MsgHandler) error

	// Unsubscribe 取消订阅
	Unsubscribe(subject string) error

	// QueueSubscribe 队列订阅
	QueueSubscribe(subject, queue string, callback nats.MsgHandler) error

	// Close 关闭NATS连接
	Close()
}

// IPublisher 定义发布者接口
type IPublisher interface {
	// PublishJSON 将对象编码为JSON并发布
	PublishJSON(subject string, data interface{}) error

	// PublishMessage 发布标准格式的消息
	PublishMessage(topic string, source string, data interface{}) error

	// RequestJSON 发送请求并等待JSON响应
	RequestJSON(subject string, data interface{}, response interface{}, timeout time.Duration) error
}

// ISubscriber 定义订阅者接口
type ISubscriber interface {
	// Subscribe 订阅主题
	Subscribe(topic string, handler MessageHandler) error

	// SubscribeFunc 使用函数订阅主题
	SubscribeFunc(topic string, handlerFunc func(msg *Message) error) error

	// QueueSubscribe 队列订阅主题
	QueueSubscribe(topic, queue string, handler MessageHandler) error

	// QueueSubscribeFunc 使用函数进行队列订阅
	QueueSubscribeFunc(topic, queue string, handlerFunc func(msg *Message) error) error

	// Unsubscribe 取消订阅
	Unsubscribe(topic string) error

	// SubscribeJSON 订阅JSON消息并自动解析到指定类型
	SubscribeJSON(topic string, targetType interface{}, handler func(data interface{}) error) error
}

// INatsServer 定义NATS服务器接口
type INatsServer interface {
	// RegisterHandler 注册消息处理器
	RegisterHandler(subject string, handler nats.MsgHandler) error

	// RegisterQueueHandler 注册队列处理器
	RegisterQueueHandler(subject, queue string, handler nats.MsgHandler) error

	// UnregisterHandler 取消注册处理器
	UnregisterHandler(subject string) error

	// Publish 发布消息
	Publish(subject string, data []byte) error

	// Close 关闭NATS服务器
	Close()
}
